(function($) {
  $.fn.outerHTML = function() {
    return $(this).clone().wrap('<div></div>').parent().html();
  };
})(jQuery);
$(window).ready(function(){
	var i=false;
	var desktop;
	var p;
	var mobile;
	type="expiration";
	vdata={"type":type};
	$.ajax({
		type: "POST",
		url: './RetourAjax/Notifications.php',
		data: vdata,
		dataType: "json",
		encode:true
	}).done(function( data ) {
		makeNotifDesk(data.desktop);
		makeNotifMobile(data.mobile);
		makeNotifMobileHD(data.mobileHD);
		makeNotifmajcron(data.DateMAJCron);
		IncNotif();
		activebutton(data);
	});
	p=$(".notification-center");
	p.css("left","-"+p.outerWidth());
	$(".notification").click(function(){
		$(".notification-center").toggleClass("active");
		p.css("left","-"+p.outerWidth());
		p=$(".notification-center.active");
		p.css("left","0");
		$(".arrow").toggleClass("glyphicon-chevron-left");
		$(".arrow").toggleClass("glyphicon-chevron-right");
	})
})
function builgrowl(){
	template='<div data-growl="container" class="alert" role="alert"><span data-growl="icon"></span><span data-growl="title"></span><br><span data-growl="message"></span><span data-growl="seemore"></span></div>'
	return $(template);
}

function addmodallicence(data){
	var content="";
	template=$("<table class='table table-striped table-condensed'><thead><tr><th>#</th><th>Nom</th><th>Prénom</th><th>&nbsp;</th></tr></thead><tbody data-table='tbody'></tbody></table>");
	templaterow=$("<tr><td data-row='id'></td><td data-row='nom'></td><td data-row='prenom'></td><td><form method='post' action='editClients.php'><button type='submit' class='btn btn-primary'>Editer</button><input type='hidden' name='idPrat' ></form></td></tr>");
	for(cpt=0; cpt<data.length; cpt++){
		templaterow.find('[data-row="id"]').html(data[cpt].praticien);
		templaterow.find('[data-row="nom"]').html(data[cpt].nom);
		templaterow.find('[data-row="prenom"]').html(data[cpt].prenom);
		templaterow.find('input[name="idPrat"]').val(data[cpt].praticien);
		add= template.find('[data-table="tbody"]').html();
		add+=templaterow.outerHTML();
		template.find('[data-table="tbody"]').html(add);
	}
	
	bootbox.alert(template.outerHTML());
}
function addcontent(template, title, msg, icon, option){
	template.find('[data-growl="title"]').html(title);
	template.find('[data-growl="message"]').html(msg);
	glyphicon="glyphicon-warning-sign";
	if(icon=="success"){
		glyphicon="glyphicon-ok";
	}
	template.find('[data-growl="icon"]').addClass("glyphicon "+glyphicon);
	if(option){
		template.find('[data-growl="seemore"]').addClass("glyphicon glyphicon-plus-sign icontoggle seemore"+option);
		template.find('[data-growl="seemore"]').attr("title", "Cliquer pour en savoir plus");
	}
	template.addClass("[ col-xs-12 ] alert alert-"+icon+" growl-animated animated fadeInDown");
}
function placement(template, location){
	$(location).append(template);
}
function makeNotifDesk(data){
	cpt= data.length;
	title="Licences Expirées"
	msg = "Attention il y a "+cpt+" licences Orémia Desktop arrivées à expiration";
	template=builgrowl();
	addcontent(template, title, msg, "warning", "desktop");
	placement(template, ".growl-container");
}
function makeNotifMobile(data){
	cpt= data.length;
	title="Licences Expirées"
	msg = "Attention il y a "+cpt+" licences Orémia Mobile arrivées à expiration";
	template=builgrowl();
	addcontent(template, title, msg, "warning", "mobile");
	placement(template, ".growl-container");
}
function makeNotifMobileHD(data){
	cpt= data.length;
	title="Licences Expirées"
	msg = "Attention il y a "+cpt+" licences Orémia Mobile HD arrivées à expiration";
	template=builgrowl();
	addcontent(template, title, msg, "warning", "mobileHD");
	placement(template, ".growl-container");
}
function makeNotifmajcron(data){
	n= new Date();
	d=new Date(data);
	datemaj=d.getUTCFullYear()+""+(d.getUTCMonth()+1)+""+d.getDate();
	now=n.getUTCFullYear()+""+(n.getUTCMonth()+1)+""+n.getDate();
	title="Mise à jour sms";
	if(datemaj!=now){
		msg = "L'execution du cron n'a pas eu lieu dernière mise à jour: "+data;
		state="danger";
	}else{
		msg = "Le cron s'est bien éxécuté le "+data+" avec succès";
		state="success";
	}
	template=builgrowl();
	addcontent(template, title, msg, state);
	placement(template, ".growl-container");
}
function IncNotif(){
	count= $('[data-growl="container"]').length;
	$(".badge").html(count);
}
function activebutton(data){
	desktop=data.desktop
	$(".seemoredesktop").click(function(){
		addmodallicence(desktop);
	});
	mobile=data.mobile
	$(".seemoremobile").click(function(){
		addmodallicence(mobile);
	});
	mobileHD=data.mobileHD
	$(".seemoremobileHD").click(function(){
		addmodallicence(mobileHD);
	});
}